package com.dingding.web.activity;

import javax.annotation.Resource;
import com.dingding.common.core.base.dto.FacadeResult;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.facde.activity.dto.commonactivity.ResRegFromActivity;
import com.dingding.facde.activity.service.ICommonActivityFacade;
import com.dingding.web.base.action.BaseAction;

/**
 * @Description 圣诞活动
 * @author fengshuonan
 * @date 2016年12月22日 上午11:04:15
 */
@SuppressWarnings("serial")
public class ShengDanAction extends BaseAction {

	// 活动相关接口
	@Resource(name = "commonActivityFacade")
	private ICommonActivityFacade commonActivityFacade;

	/**
	 * @Description 获取优惠券
	 * @author fengshuonan
	 */
	public String getCoupon() {

		String userId = super.getPara("userId");
		String activityId = super.getPara("activityId");

		// 验证请求参数是否为空
		if (ToolUtil.validateParas(userId, activityId)) {
			super.writeNullParaError();
			return NONE;
		}

		// 判断是否活动已经结束，活动结束就不走注册业务(结束包括日期期限到了，还有优惠券领取完)
		// 判断活动是否结束返回的状态(1:优惠券领取完毕 4：活动到期 0：活动还在进行 5:异常)
		FacadeResult<ResRegFromActivity> result = FacadeResult.SUCCESS(new ResRegFromActivity());
		int judgeActivityFinish = super.judgeActivityFinish(activityId);
		if (judgeActivityFinish == 1) {
			result.getObject().setUserType(1);// 状态1是，提示优惠券发放完
			super.writeFacade(result);
			return NONE;
		} else if (judgeActivityFinish == 4) {
			result.getObject().setUserType(4);// 状态4是，提示活动结束
			super.writeFacade(result);
			return NONE;
		} else if (judgeActivityFinish == 5) {
			result.getObject().setUserType(5);// 状态5是，提示服务器异常
			super.writeFacade(result);
			return NONE;
		}

		// 调用接口，判断是否领取过(3:已经领取过优惠券 2：没有领取过优惠券 5：异常)
		int judgeUserIsGotCoupon = super.judgeUserIsGotCoupon(activityId, userId);

		if (judgeUserIsGotCoupon == 3) {
			result.getObject().setUserType(3);// 已经领取过优惠券
			super.writeFacade(result);
			return NONE;
		} else if (judgeUserIsGotCoupon == 2) {

			// 调用接口，领取优惠券，增加用户一个优惠券
			int joinActivity = this.joinActivity(userId);
			if (joinActivity == 2) {
				result.getObject().setUserType(2);// 没有领取过优惠券,领取成功
				super.writeFacade(result);
				return NONE;

				// 领取失败
			} else {
				result.getObject().setUserType(5);// 领取失败
				super.writeFacade(result);
				return NONE;
			}

		} else {
			result.getObject().setUserType(5);// dubbo 判断用户是否领取异常
			super.writeFacade(result);
			return NONE;
		}
	}
}
